package org.example.reemploymentsys.service;

import org.example.reemploymentsys.entity.Organization;
import org.example.reemploymentsys.entity.TrainingDocument;
import org.example.reemploymentsys.repository.OrganizationRe;
import org.example.reemploymentsys.repository.TrainingDocumentRe;
import org.example.reemploymentsys.repository.UserTbRe;
import org.example.reemploymentsys.tool.OssOpe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class DocumentService {
    @Autowired
    private TrainingDocumentRe documentRe;
    @Autowired
    private OssOpe ossOpe;
    @Autowired
    private OrganizationRe organizationRe;
    @Autowired
    private UserTbRe userTbRe;

    public List<TrainingDocument> getAllDocuments(String username,String type, Boolean underReview, Boolean published) {
        Specification<TrainingDocument> spec = Specification.where(null);
        Long OrganizationId= Long.valueOf(userTbRe.findOrgizationIdByUsername(username));
        String roleName="superAdmin";
        // 动态添加查询条件
        if (type != null) {
            spec = spec.and((root, query, builder) -> builder.equal(root.get("type"), type));
        }
        if (underReview != null) {
            spec = spec.and((root, query, builder) -> builder.equal(root.get("UnderReview"), underReview));
        }

        if (published != null) {
            spec = spec.and((root, query, builder) -> builder.equal(root.get("Published"), published));
        }
       List<TrainingDocument> documents = documentRe.findAll(spec);
        List<TrainingDocument> results=new ArrayList<>();
        for (TrainingDocument document : documents) {
            if(document.getOrganization()==null){
                if (document.getRole().getRoleName().equals(roleName)) {
                    results.add(document);
                }
            }
            else if(document.getRole().getOrganization().getId() == OrganizationId){
                results.add(document);
            }
        }

        // 执行查询
        return results;
    }

    public TrainingDocument addDocument(String OrganizationId,TrainingDocument document) {
        Organization organization =organizationRe.findOrganizationById(OrganizationId);
        document.setOrganization(organization);
        return documentRe.save(document);
    }

    public String deleteDocument(Long id) {
        try {
            TrainingDocument document= documentRe.findById(id).get();
            documentRe.deleteById(id);
            String url = document.getUrl();
            ossOpe.deleteFile(url);
            return "true";
        } catch (Exception e) {
            return e.getMessage();
        }
    }
    public TrainingDocument updateDocument(Long id, TrainingDocument updatedDocument) {
        String OrId= String.valueOf(updatedDocument.getRole().getOrganization().getId());
        Organization organization =organizationRe.findOrganizationById(OrId);
        updatedDocument.setOrganization(organization);
        return documentRe.save(updatedDocument);
    }

    public TrainingDocument getDocuments(Long id) {
        return documentRe.findById(id).orElse(null);
    }
}
